import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'hot_widget.dart';
import 'love_widget.dart';

class TiktokWidget extends StatefulWidget {
  final String ?text;

  TiktokWidget({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return TiktokState();
  }
}

class TiktokState extends State<TiktokWidget>
    with SingleTickerProviderStateMixin {
  TabController ?_tabController; //需要定义一个Controller
  List tabs = ["喜欢", "热门"];
  String? title; //标题
  var _selectedIndex = 0;

  List<Widget> getTabViews(BuildContext context) => <Widget>[
        LoveWidget(),
        HotWidget(),
      ];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //背景色
      backgroundColor: Colors.black,
      //左上标题
      appBar: AppBar(
        title: Text(widget.text??""),
        bottom: TabBar(
          //生成Tab菜单
          controller: _tabController,
          //标签选择变化控制器
          tabs: tabs.map((e) => Tab(text: e)).toList(),
          onTap: _onItemTapped,
          indicatorSize: TabBarIndicatorSize.label,
          labelStyle: TextStyle(fontSize: 18),
          labelColor: Colors.white,
          //标签颜色
          isScrollable: true,
          indicatorColor: Colors.white,
          // indicator: const BoxDecoration(), //,//用于设定选中状态下的展示样式不要下划线
        ),
        backgroundColor: Colors.black,
      ),
      body: TabBarView(
        physics: NeverScrollableScrollPhysics(), //禁止滑动
        //联动
        controller: _tabController,
        children: getTabViews(context).map((Widget widget) {
          return widget;
        }).toList(),
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }
}
